package com.lmf.mock.starter.autoconfigure.controller;

import com.lmf.mock.starter.autoconfigure.entity.ProxyConfig;
import com.lmf.mock.starter.autoconfigure.service.ProxyConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理配置管理控制器
 */
@RestController
@RequestMapping("/proxy/config")
public class ProxyConfigController {

    @Autowired
    private ProxyConfigService proxyConfigService;

    /**
     * 获取所有代理配置
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProxyConfig> configs = proxyConfigService.list();
            result.put("success", true);
            result.put("data", configs);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取代理配置列表失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 根据分组ID获取代理配置
     */
    @GetMapping("/list/{groupId}")
    public ResponseEntity<Map<String, Object>> listByGroup(@PathVariable Long groupId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProxyConfig> configs = proxyConfigService.getConfigsByGroupId(groupId);
            result.put("success", true);
            result.put("data", configs);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取代理配置列表失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取代理配置详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProxyConfig config = proxyConfigService.getConfigById(id);
            if (config != null) {
                result.put("success", true);
                result.put("data", config);
            } else {
                result.put("success", false);
                result.put("message", "代理配置不存在");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取代理配置详情失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 创建代理配置
     */
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> create(@RequestBody ProxyConfig config) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProxyConfig created = proxyConfigService.createConfig(config);
            result.put("success", true);
            result.put("data", created);
            result.put("message", "代理配置创建成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "创建代理配置失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 更新代理配置
     */
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> update(@RequestBody ProxyConfig config) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProxyConfig updated = proxyConfigService.updateConfig(config);
            result.put("success", true);
            result.put("data", updated);
            result.put("message", "代理配置更新成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新代理配置失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 删除代理配置
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean deleted = proxyConfigService.deleteConfig(id);
            if (deleted) {
                result.put("success", true);
                result.put("message", "代理配置删除成功");
            } else {
                result.put("success", false);
                result.put("message", "代理配置删除失败");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除代理配置失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 切换代理配置状态
     */
    @PostMapping("/{id}/toggle")
    public ResponseEntity<Map<String, Object>> toggleStatus(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean toggled = proxyConfigService.toggleConfigStatus(id);
            if (toggled) {
                result.put("success", true);
                result.put("message", "代理配置状态切换成功");
            } else {
                result.put("success", false);
                result.put("message", "代理配置状态切换失败");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "切换代理配置状态失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
}
